<?php
/**
 * Created by PhpStorm.
 * User: wk
 * Date: 2018/4/11
 * Time: 11:29
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\view;
class Role extends Admincontroller
{

    public function index()
    {
//        echo '123';die;
        $result=Db::table('role')
            ->field(['id','name','remark','status'])
            ->select();
//        var_dump($result);die;
        return view('role/index',['data'=>$result]);
//
    }
    public function addrole()
    {
        return view('role/add');
    }

    public function doaddrole(Request $request)
    {

//        var_dump($request);exit;
        $data=$request->post();
        $info['name'] =$data['name'];
        $info['remark'] =$data['remark'];
        $info['status'] =$data['status'];
        $result=Db::table('role')
            ->insert($info);
//        var_dump($result);die;
        if (!empty($result)){
            $this->success('添加成功','admin/Role/index');
        }else{
            $this->error('添加失败');
        }
    }

    public function nodelist($id)
    {
//        var_dump($id);die;
        //查找该角色信息

         $nid=Db::table('role_node')
             ->field(['nid'])
             ->where(['rid'=>$id])
             ->select($id);
//        var_dump($nid);die;
        $nodes=array();
        foreach ($nid as $v){
//            var_dump($v);die;
            $node=Db::table('node')
                ->field(['name'])
                ->where(['id'=>$v['nid']])
                ->find();
//            var_dump($role);die;
            $nodes[]=$node['name'];
        }
//        var_dump($roles);die;
//        var_dump($roles);die;
        $res=Db::table('node')
            ->field(['name','id'])
            ->select();
//        var_dump($res);die;
        return view('role/nodelist',['list'=>$res,'nodes'=>$nodes,'id'=>$id]);


    }

    public function donodelist(Request $request)
    {
        $id=$request->post();
        //判读必须选择一个角色
//        var_dump($id);die;
        if(empty($id['nodes'])){
            $this->error("请选择一个角色！");
        }

//            var_dump($id);die;
        //清除用户所有的角色信息，避免重复添加
        $del=Db::table('role_node')
            ->where(['rid'=>$id['rid']])
            ->delete();
//            var_dump($del);die;
        foreach($id['nodes'] as $v){
//                var_dump($v);die;
            $data['rid']=$id['rid'];
            $data['nid']=$v;
//                var_dump($data);die;
            $res=Db::table('role_node')
                ->insertGetId($data);
//                var_dump($res);die;
        }

        $this->success('添加成功','admin/Role/index');
//            var_dump($result);
    }
    public function roledel($id)
    {
//        var_dump($id);
        $res=Db::table('role')
            ->where(['id'=>$id])
            ->delete();
        $res1=Db::table('role_node')
            ->where(['rid'=>$id])
            ->delete();
//          var_dump($res);
        if (!empty($res)&&!empty($res)){
            $this->success('删除成功','admin/User/userlist');
        }else{
            $this->error('删除失败');
        }
    }


    public function roleedit($id)
    {
        $res=Db::table('role')
            ->field(['name','remark'])
            ->where(['id'=>$id])
            ->find();
//        var_dump($res);
        return view('role/edit',['id'=>$id,'list'=>$res]);
    }
    public function doroleedit($id)
    {
//        var_dump($_POST);
        $data=$_POST;
        $res=Db::table('role')
            ->where(['id'=>$id])
            ->update($data);
//        var_dump($res);
        if ($res){
            $this->success('添加成功','admin/Role/index');
        }else{
            $this->error('添加失败');
        }

    }

}